package io.finer.erp.jeecg.bas.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;

import io.finer.erp.jeecg.bas.entity.BasBankAccount;
import io.finer.erp.jeecg.bas.entity.BasCustomer;
import io.finer.erp.jeecg.bas.entity.BasSupplier;
import io.finer.erp.jeecg.bas.entity.Fukuandan;
import io.finer.erp.jeecg.bas.entity.Kemu;
import io.finer.erp.jeecg.bas.entity.Shoukuandan;
import io.finer.erp.jeecg.bas.entity.Yingfudan;
import io.finer.erp.jeecg.bas.entity.Yingshoudan;
import io.finer.erp.jeecg.bas.entity.ZrkPiecerate;
import io.finer.erp.jeecg.bas.service.IBasBankAccountService;
import io.finer.erp.jeecg.bas.service.IBasCustomerService;
import io.finer.erp.jeecg.bas.service.IBasSupplierService;
import io.finer.erp.jeecg.bas.service.IFukuandanService;
import io.finer.erp.jeecg.bas.service.IKemuService;
import io.finer.erp.jeecg.bas.service.IShoukuandanService;
import io.finer.erp.jeecg.bas.service.IYingfudanService;
import io.finer.erp.jeecg.bas.service.IYingshoudanService;
import io.finer.erp.jeecg.bas.service.IZrkFqjfService;
import io.finer.erp.jeecg.bas.service.IZrkPiecerateService;
import io.finer.erp.jeecg.finance.entity.FinPayable;
import io.finer.erp.jeecg.finance.entity.FinPayment;
import io.finer.erp.jeecg.finance.entity.FinReceipt;
import io.finer.erp.jeecg.finance.entity.FinReceivable;
import io.finer.erp.jeecg.finance.service.IFinPayableService;
import io.finer.erp.jeecg.finance.service.IFinPaymentService;
import io.finer.erp.jeecg.finance.service.IFinReceiptService;
import io.finer.erp.jeecg.finance.service.IFinReceivableService;
import io.finer.erp.jeecg.stock.service.IProcessingRecordService;
import io.finer.erp.jeecg.stock.service.IStkInventoryService;
import io.finer.erp.jeecg.stock.service.IStkIoBillEntryService;
import io.finer.erp.jeecg.stock.service.IStkIoBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description: 计件工资表
 * @Author: jeecg-boot
 * @Date:   2020-08-19
 * @Version: V1.0
 */
@Api(tags="计件工资表")
@RestController
@RequestMapping("/bas/zrkPiecerate")
@Slf4j
public class ZrkPiecerateController extends JeecgController<ZrkPiecerate, IZrkPiecerateService> {
	@Autowired
	private IZrkPiecerateService zrkPiecerateService;
	@Autowired
	private IZrkFqjfService zrkFqjfService;

	@Autowired
	private IStkIoBillService stkIoBillService;
	@Autowired
	private IStkIoBillEntryService stkIoBillEntryService;
	//	@Autowired
	//	private IZrkKindeeProofCgService zrkKindeeProofCgService;//采购
	//	@Autowired
	//	private IZrkKindeeProofDjgService zrkKindeeProofDjgService;//代加工
	//	@Autowired
	//	private IZrkKindeeProofFhService zrkKindeeProofFhService;//复合
	//	@Autowired
	//	private IZrkKindeeProofFlService zrkKindeeProofFlService;//辅料
	//	@Autowired
	//	private IZrkKindeeProofFqService zrkKindeeProofFqService;//分切
	//	@Autowired
	//	private IZrkKindeeProofQzService zrkKindeeProofQzService;//切张
	//	@Autowired
	//	private IZrkKindeeProofSkService zrkKindeeProofSkService;//收款
	//	@Autowired
	//	private IZrkKindeeProofThService zrkKindeeProofThService;//退货
	//	@Autowired
	//	private IZrkKindeeProofXsckService zrkKindeeProofXsckService;//销售出库
	//	@Autowired
	//	private IZrkKindeeProofZjService zrkKindeeProofZjService;//制胶
	@Autowired
	private IBasSupplierService basSupplierService;
	@Autowired
	private IFinPayableService finPayableService;
	@Autowired
	private IFinPaymentService finPaymentService;
	@Autowired
	private IBasBankAccountService basBankAccountService;
	@Autowired
	private IProcessingRecordService processingRecordServie;
	@Autowired
	private IStkInventoryService stkInventoryService;
	@Autowired
	private IBasCustomerService basCustomerService;
	@Autowired
	private IFinReceivableService finReceivableService;
	@Autowired
	private IFinReceiptService finReceiptService;
	@Autowired
	private IShoukuandanService shoukuandanService;
	@Autowired
	private IYingshoudanService yingshoudanService;
	@Autowired
	private IFukuandanService fukuandanService;
	@Autowired
	private IYingfudanService yingfudanService;
	@Autowired
	private IKemuService kemuService;

	/**
	 * 分页列表查询
	 *
	 * @param zrkPiecerate
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "计件工资表-分页列表查询")
	@ApiOperation(value="计件工资表-分页列表查询", notes="计件工资表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ZrkPiecerate zrkPiecerate,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		Page<ZrkPiecerate> page = new Page<ZrkPiecerate>(pageNo, pageSize);
		IPage<ZrkPiecerate> pageList = zrkPiecerateService.getPageList(page, zrkPiecerate);
		return Result.ok(pageList);
	}

	/**
	 *   添加
	 *
	 * @param zrkPiecerate
	 * @return
	 */
	@AutoLog(value = "计件工资表-添加")
	@ApiOperation(value="计件工资表-添加", notes="计件工资表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ZrkPiecerate zrkPiecerate) {
//		BigDecimal total=new BigDecimal("0");
		//统计该条记录的合计薪资(切张计算)
		//		ZrkQz zrkQz=new ZrkQz();
		//		zrkQz.setQzType(zrkPiecerate.getZrkType().toString());
		//查询切张参数
//		Map<String,Object> queryMap=new HashMap<String,Object>();
//		queryMap.put("qz_type",zrkPiecerate.getZrkType());
//		Collection<ZrkQz> qzList=zrkQzService.listByMap(queryMap);
//		Iterator<ZrkQz> iterator = qzList.iterator();
//		int i=0;
//		if(i==0) {
//			while(iterator.hasNext()) {
//				ZrkQz qz=iterator.next();
//				//计算普通切张工资
//				BigDecimal tsMoney=zrkPiecerate.getZrkTs().multiply(new BigDecimal(qz.getQzTs()));
//				BigDecimal zsMoney=zrkPiecerate.getZrkQzzs().multiply(new BigDecimal(qz.getQzZs()));
//				BigDecimal pfMoney=zrkPiecerate.getZrkQzpf().multiply(new BigDecimal(qz.getQzPf()));
//				BigDecimal totalMoney=tsMoney.add(zsMoney).add(pfMoney);
//				total=total.add(totalMoney);  
//			}
//		}
//		//分切薪资计算
//		//		ZrkFqjf zrkFq=new ZrkFqjf();
//		//		zrkFq.setMachineType(zrkPiecerate.getZrkJqbh());
//		//查询分切单价参数
//		Map<String,Object> queryMapFQ=new HashMap<String,Object>();
//		queryMapFQ.put("machine_type",zrkPiecerate.getZrkJqbh());
//		Collection<ZrkFqjf> fqList=zrkFqjfService.listByMap(queryMapFQ);
//		int j=0;
//		Iterator<ZrkFqjf> iter=fqList.iterator();
//		if(j==0) {
//			while(iter.hasNext()) {
//				ZrkFqjf fq=iter.next();
//				BigDecimal dsMoney=zrkPiecerate.getZrkFqds().multiply(new BigDecimal(fq.getDsPrice()));
//				BigDecimal tdsMoney=zrkPiecerate.getZrkFqtds().multiply(new BigDecimal(fq.getTdsPrice()));
//				BigDecimal fqPfMoney=zrkPiecerate.getZrkFqpf().multiply(new BigDecimal(fq.getPfPrice()));
//				BigDecimal fqTotal=dsMoney.add(tdsMoney).add(fqPfMoney);
//				BigDecimal totalRealMoney=total.add(fqTotal);
//				zrkPiecerate.setZrkTotalmoney(totalRealMoney);//统计该条记录薪资后设置实体bean
//			}
//		}
		zrkPiecerateService.save(zrkPiecerate);
		return Result.ok("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param zrkPiecerate
	 * @return
	 */
	@AutoLog(value = "计件工资表-编辑")
	@ApiOperation(value="计件工资表-编辑", notes="计件工资表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ZrkPiecerate zrkPiecerate) {
		zrkPiecerateService.updateById(zrkPiecerate);
		return Result.ok("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "计件工资表-通过id删除")
	@ApiOperation(value="计件工资表-通过id删除", notes="计件工资表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		zrkPiecerateService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 *  销售预收凭证生成
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "销售预收凭证生成")
	@ApiOperation(value="销售预收凭证生成", notes="销售预收凭证生成")
	@GetMapping(value = "/generateXSYS")
	public Result<?> generateXSYS(@RequestParam(name="ids",required=true) String ids,@RequestParam(name="kjkm1",required=true) String kjkm1,@RequestParam(name="kjkm2",required=true) String kjkm2){
		List<String> lis=Arrays.asList(ids.split(","));
		String compName="";
		String bankCardNo="";
		String bankname="";
		//查询本企业银行卡信息
		List<BasBankAccount> bbaList=basBankAccountService.list();
		if(null!=bbaList&&bbaList.size()>0) {
			for(BasBankAccount ba:bbaList) {
				if(ba.getName().indexOf("启元")>0) {
					compName=ba.getName();
					bankCardNo=ba.getAccountNo();
					bankname=ba.getBankNo();
				}
			}
		}
		//销售预收单--凭证
		QueryWrapper<FinReceipt> fpQw2=new QueryWrapper<FinReceipt>();
		fpQw2.in("id", lis);
		List<FinReceipt> fpmentList=finReceiptService.list(fpQw2);
		//查询借贷会计科目
		String acJId="";
		String acJName="";
		String acJCode="";
		String acDId="";
		String acDName="";
		String acDCode="";
		Kemu kmj=kemuService.getById(kjkm1);
		Kemu kmd=kemuService.getById(kjkm2);
		if(null!=kmj) {
			acJId=kmj.getId();
			acJName=kmj.getKmmc();
			acJCode=kmj.getKmbh();
		}
		if(null!=kmd) {
			acDId=kmd.getId();
			acDName=kmd.getKmmc();
			acDCode=kmd.getKmbh();
		}
		//非空
		if(null!=fpmentList&&fpmentList.size()>0) {
			for(int j=0;j<fpmentList.size();j++) {
				FinReceipt fp=fpmentList.get(j);
				//是否生成过凭证
				if(fp.getIsUpload()==0) {
					//查询客户信息
					BasCustomer bs=basCustomerService.getById(fp.getCustomerId());
					String sId="";
					String sName="";
					try {
						sId=bs.getId();
						sName=bs.getName();
					} catch (Exception e) {
					}
					//构造中间表数据--借
					Shoukuandan zkcg2=new Shoukuandan();
					zkcg2.setCreateBy("admin");
					zkcg2.setCreateTime(fp.getCreateTime());
					zkcg2.setBillNo(fp.getBillNo());
					zkcg2.setBillType("销售预收款单");
					zkcg2.setBillMoney(fp.getAmt());
					zkcg2.setAccountingType(acJName);
					zkcg2.setAccountingId(acJId);
					zkcg2.setKindeeType("借");
					zkcg2.setGroupId((j+1)+"");
					zkcg2.setCustomerId(sId);
					zkcg2.setCustomerName(sName);
					//构造中间表数据--贷
					Shoukuandan zkcg3=new Shoukuandan();
					zkcg3.setCreateBy("admin");
					zkcg3.setCreateTime(fp.getCreateTime());
					zkcg3.setBillNo(fp.getBillNo());
					zkcg3.setBillType("销售预收款单");
					zkcg3.setBillMoney(fp.getAmt());
					zkcg3.setAccountingType(acDName);
					zkcg3.setAccountingId(acDId);
					zkcg3.setKindeeType("贷");
					zkcg3.setGroupId((j+1)+"");
					zkcg3.setCustomerId(sId);
					zkcg3.setCustomerName(sName);
					//保存数据
					shoukuandanService.save(zkcg2);
					shoukuandanService.save(zkcg3);
					fp.setIsUpload(1);
					finReceiptService.updateById(fp);
				}else {
					return Result.error("请不要重复生成凭证");
				}
			}
			return Result.ok("生成凭证成功");
		}else {
			return Result.error("未找到预收款记录");
		}
	}

	/**
	 *  销售收款凭证生成
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "销售收款凭证生成")
	@ApiOperation(value="销售收款凭证生成", notes="销售收款凭证生成")
	@GetMapping(value = "/generateXSSK")
	public Result<?> generateXSSK(@RequestParam(name="ids",required=true) String ids,@RequestParam(name="kjkm1",required=true) String kjkm1,@RequestParam(name="kjkm2",required=true) String kjkm2){
		List<String> lis=Arrays.asList(ids.split(","));
		String compName="";
		String bankCardNo="";
		String bankname="";
		//查询本企业银行卡信息
		List<BasBankAccount> bbaList=basBankAccountService.list();
		if(null!=bbaList&&bbaList.size()>0) {
			for(BasBankAccount ba:bbaList) {
				if(ba.getName().indexOf("启元")>0) {
					compName=ba.getName();
					bankCardNo=ba.getAccountNo();
					bankname=ba.getBankNo();
				}
			}
		}
		//查询借贷会计科目
		String acJId="";
		String acJName="";
		String acJCode="";
		String acDId="";
		String acDName="";
		String acDCode="";
		Kemu kmj=kemuService.getById(kjkm1);
		Kemu kmd=kemuService.getById(kjkm2);
		if(null!=kmj) {
			acJId=kmj.getId();
			acJName=kmj.getKmmc();
			acJCode=kmj.getKmbh();
		}
		if(null!=kmd) {
			acDId=kmd.getId();
			acDName=kmd.getKmmc();
			acDCode=kmd.getKmbh();
		}
		//销售收款单--凭证
		QueryWrapper<FinReceipt> fpQw3=new QueryWrapper<FinReceipt>();
		fpQw3.in("id", lis);
		List<FinReceipt> fpment2List=finReceiptService.list(fpQw3);
		//非空
		if(null!=fpment2List&&fpment2List.size()>0) {
			for(int j=0;j<fpment2List.size();j++) {
				FinReceipt fp=fpment2List.get(j);
				//未生成过凭证
				if(fp.getIsUpload()==0) {
					//查询客户信息
					BasCustomer bs=basCustomerService.getById(fp.getCustomerId());
					String sId="";
					String sName="";
					try {
						sId=bs.getId();
						sName=bs.getName();
					} catch (Exception e) {
					}
					//				zrkKendeeProofSkdService
					//构造中间表数据--借
					Shoukuandan zkcg2=new Shoukuandan();
					zkcg2.setCreateBy("admin");
					zkcg2.setCreateTime(fp.getCreateTime());
					zkcg2.setBillNo(fp.getBillNo());
					zkcg2.setBillType("销售收款单");
					zkcg2.setBillMoney(fp.getAmt());
					zkcg2.setKindeeType("借");
					zkcg2.setGroupId((j+1)+"");
					zkcg2.setCustomerId(sId);
					zkcg2.setCustomerName(sName);
					zkcg2.setAccountingType(acJName);
					zkcg2.setAccountingId(acJId);
					//构造中间表数据--贷
					Shoukuandan zkcg3=new Shoukuandan();
					zkcg3.setCreateBy("admin");
					zkcg3.setCreateTime(fp.getCreateTime());
					zkcg3.setBillNo(fp.getBillNo());
					zkcg3.setBillType("销售收款单");
					zkcg3.setBillMoney(fp.getAmt());
					zkcg3.setKindeeType("贷");
					zkcg3.setGroupId((j+1)+"");
					zkcg3.setCustomerId(sId);
					zkcg3.setCustomerName(sName);
					zkcg3.setAccountingType(acDName);
					zkcg3.setAccountingId(acDId);
					//保存数据
					shoukuandanService.save(zkcg2);
					shoukuandanService.save(zkcg3);
					fp.setIsUpload(1);
					finReceiptService.updateById(fp);
				}else {
					return Result.error("请不要重复生成凭证");
				}
			}
		}else {
			Result.error("未找到收款单数据");
		}
		return Result.ok("收款单凭证生成成功");
	}

	/**
	 *  销售应收凭证生成
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "销售应收凭证生成")
	@ApiOperation(value="销售应收凭证生成", notes="销售应收凭证生成")
	@GetMapping(value = "/generateXSYYS")
	public Result<?> generateXSYYS(@RequestParam(name="ids",required=true) String ids,@RequestParam(name="kjkm1",required=true) String kjkm1,@RequestParam(name="kjkm2",required=true) String kjkm2){
		List<String> lis=Arrays.asList(ids.split(","));
		//查询借贷会计科目
		String acJId="";
		String acJName="";
		String acJCode="";
		String acDId="";
		String acDName="";
		String acDCode="";
		Kemu kmj=kemuService.getById(kjkm1);
		Kemu kmd=kemuService.getById(kjkm2);
		if(null!=kmj) {
			acJId=kmj.getId();
			acJName=kmj.getKmmc();
			acJCode=kmj.getKmbh();
		}
		if(null!=kmd) {
			acDId=kmd.getId();
			acDName=kmd.getKmmc();
			acDCode=kmd.getKmbh();
		}
		//销售应收单--凭证
		QueryWrapper<FinReceivable> fpQw=new QueryWrapper<FinReceivable>();
		fpQw.in("id", lis);
		List<FinReceivable> fpableList=finReceivableService.list(fpQw);
		//非空
		if(null!=fpableList&&fpableList.size()>0) {
			for(int j=0;j<fpableList.size();j++) {
				FinReceivable fp=fpableList.get(j);
				//未生成过凭证
				if(fp.getIsUpload()==0) {
					//查询客户信息
					BasCustomer bs=basCustomerService.getById(fp.getCustomerId());
					String sId="";
					String sName="";
					try {
						sId=bs.getId();
						sName=bs.getName();
					} catch (Exception e) {
					}
					//构造中间表数据--借
					Yingshoudan zkcg2=new Yingshoudan();
					zkcg2.setCreateBy("admin");
					zkcg2.setCreateTime(fp.getCreateTime());
					zkcg2.setBillNo(fp.getBillNo());
					zkcg2.setBillType("销售应收单");
					zkcg2.setBillMoney(fp.getAmt());
					zkcg2.setAccountingId(acJId);
					zkcg2.setAccountingType(acJName);
					zkcg2.setKindeeType("借");
					zkcg2.setGroupId((j+1)+"");
					zkcg2.setCustomerId(sId);
					zkcg2.setCustomerName(sName);
					//构造中间表数据--贷
					Yingshoudan zkcg3=new Yingshoudan();
					zkcg3.setCreateBy("admin");
					zkcg3.setCreateTime(fp.getCreateTime());
					zkcg3.setBillNo(fp.getBillNo());
					zkcg3.setBillType("销售应收单");
					zkcg3.setBillMoney(fp.getAmt());
					zkcg3.setAccountingId(acDId);
					zkcg3.setAccountingType(acDName);
					zkcg3.setKindeeType("贷");
					zkcg3.setGroupId((j+1)+"");
					zkcg3.setCustomerId(sId);
					zkcg3.setCustomerName(sName);
					//保存数据
					yingshoudanService.save(zkcg2);
					yingshoudanService.save(zkcg3);
					fp.setIsUpload(1);
					finReceivableService.updateById(fp);
				}else {
					return Result.error("请不要重复生成凭证");
				}
			}
			return Result.ok("生成应收单凭证成功");
		}else {
			return Result.error("未找到应收单数据");
		}
	}

	/**
	 *  采购预付凭证生成
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "采购预付凭证生成")
	@ApiOperation(value="采购预付凭证生成", notes="采购预付凭证生成")
	@GetMapping(value = "/generateYF")
	public Result<?> generateYF(@RequestParam(name="ids",required=true) String ids,@RequestParam(name="kjkm1",required=true) String kjkm1,@RequestParam(name="kjkm2",required=true) String kjkm2){
		List<String> lis=Arrays.asList(ids.split(","));
		String compName="";
		String bankCardNo="";
		String bankname="";
		//查询本企业银行卡信息
		List<BasBankAccount> bbaList=basBankAccountService.list();
		if(null!=bbaList&&bbaList.size()>0) {
			for(BasBankAccount ba:bbaList) {
				if(ba.getName().indexOf("启元")>0) {
					compName=ba.getName();
					bankCardNo=ba.getAccountNo();
					bankname=ba.getBankNo();
				}
			}
		}
		//查询借贷会计科目
		String acJId="";
		String acJName="";
		String acJCode="";
		String acDId="";
		String acDName="";
		String acDCode="";
		Kemu kmj=kemuService.getById(kjkm1);
		Kemu kmd=kemuService.getById(kjkm2);
		if(null!=kmj) {
			acJId=kmj.getId();
			acJName=kmj.getKmmc();
			acJCode=kmj.getKmbh();
		}
		if(null!=kmd) {
			acDId=kmd.getId();
			acDName=kmd.getKmmc();
			acDCode=kmd.getKmbh();
		}
		//采购预付单--凭证
		QueryWrapper<FinPayment> fpQw2=new QueryWrapper<FinPayment>();
		fpQw2.in("id", lis);
		List<FinPayment> fpmentList=finPaymentService.list(fpQw2);
		//非空
		if(null!=fpmentList&&fpmentList.size()>0) {
			for(int j=0;j<fpmentList.size();j++) {
				FinPayment fp=fpmentList.get(j);
				//未生成过凭证
				if(fp.getIsUpload()==0) {
					//查询供应商信息
					BasSupplier bs=basSupplierService.getById(fp.getSupplierId());
					String sId="";
					String sName="";
					try {
						sId=bs.getId();
						sName=bs.getName();
					} catch (Exception e) {
					}
					//构造中间表数据--借
					Fukuandan zkcg2=new Fukuandan();
					zkcg2.setCreateBy("admin");
					zkcg2.setCreateTime(fp.getCreateTime());
					zkcg2.setBillNo(fp.getBillNo());
					zkcg2.setBillType("采购预付单");
					zkcg2.setBillMoney(fp.getAmt());
					zkcg2.setAccountingId(acJId);
					zkcg2.setAccountingType(acJName);
					zkcg2.setKindeeType("借");
					zkcg2.setGroupId((j+1)+"");
					zkcg2.setSupplierId(bs.getId());
					zkcg2.setSupplierName(bs.getName());
					//构造中间表数据--贷
					Fukuandan zkcg3=new Fukuandan();
					zkcg3.setCreateBy("admin");
					zkcg3.setCreateTime(fp.getCreateTime());
					zkcg3.setBillNo(fp.getBillNo());
					zkcg3.setBillType("采购预付单");
					zkcg3.setBillMoney(fp.getAmt());
					zkcg3.setAccountingId(acDId);
					zkcg3.setAccountingType(acDName);
					zkcg3.setKindeeType("贷");
					zkcg3.setGroupId((j+1)+"");
					zkcg3.setSupplierId(sId);
					zkcg3.setSupplierName(sName);
					//保存数据
					fukuandanService.save(zkcg2);
					fukuandanService.save(zkcg3);
					fp.setIsUpload(1);
					finPaymentService.updateById(fp);
				}else {
					return Result.error("请不要重复生成凭证");
				}
			}
		}
		return Result.ok();
	}


	/**
	 *  采购应付凭证生成
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "采购应付凭证生成")
	@ApiOperation(value="采购应付凭证生成", notes="采购应付凭证生成")
	@GetMapping(value = "/generateYFD")
	public Result<?> generateYFD(@RequestParam(name="ids",required=true) String ids,@RequestParam(name="kjkm1",required=true) String kjkm1,@RequestParam(name="kjkm2",required=true) String kjkm2){
		List<String> lis=Arrays.asList(ids.split(","));
		String compName="";
		String bankCardNo="";
		String bankname="";
		//查询本企业银行卡信息
		List<BasBankAccount> bbaList=basBankAccountService.list();
		if(null!=bbaList&&bbaList.size()>0) {
			for(BasBankAccount ba:bbaList) {
				if(ba.getName().indexOf("启元")>0) {
					compName=ba.getName();
					bankCardNo=ba.getAccountNo();
					bankname=ba.getBankNo();
				}
			}
		}
		//查询借贷会计科目
		String acJId="";
		String acJName="";
		String acJCode="";
		String acDId="";
		String acDName="";
		String acDCode="";
		Kemu kmj=kemuService.getById(kjkm1);
		Kemu kmd=kemuService.getById(kjkm2);
		if(null!=kmj) {
			acJId=kmj.getId();
			acJName=kmj.getKmmc();
			acJCode=kmj.getKmbh();
		}
		if(null!=kmd) {
			acDId=kmd.getId();
			acDName=kmd.getKmmc();
			acDCode=kmd.getKmbh();
		}
		//采购应付单--凭证
		QueryWrapper<FinPayable> fpQw=new QueryWrapper<FinPayable>();
		fpQw.in("id", lis);
		List<FinPayable> fpableList=finPayableService.list(fpQw);
		//非空
		if(null!=fpableList&&fpableList.size()>0) {
			for(int j=0;j<fpableList.size();j++) {
				FinPayable fp=fpableList.get(j);
				//未生成过凭证
				if(fp.getIsUpload()==0) {
					//查询供应商信息
					BasSupplier bs=basSupplierService.getById(fp.getSupplierId());
					String sId="";
					String sName="";
					try {
						sId=bs.getId();
						sName=bs.getName();
					} catch (Exception e) {
					}
					//构造中间表数据--借
					Yingfudan zkcg2=new Yingfudan();
					zkcg2.setCreateBy("admin");
					zkcg2.setCreateTime(fp.getCreateTime());
					zkcg2.setBillNo(fp.getBillNo());
					zkcg2.setBillType("采购应付单");
					zkcg2.setBillMoney(fp.getAmt());
					zkcg2.setAccountingId(acJId);
					zkcg2.setAccountingType(acJName);
					zkcg2.setKindeeType("借");
					zkcg2.setGroupId((j+1)+"");
					zkcg2.setSupplierId(sId);
					zkcg2.setSupplierName(sName);
					//构造中间表数据--贷
					Yingfudan zkcg3=new Yingfudan();
					zkcg3.setCreateBy("admin");
					zkcg3.setCreateTime(fp.getCreateTime());
					zkcg3.setBillNo(fp.getBillNo());
					zkcg3.setBillType("采购应付单");
					zkcg3.setBillMoney(fp.getAmt());
					zkcg3.setAccountingId(acJId);
					zkcg3.setAccountingType(acJName);
					zkcg3.setKindeeType("贷");
					zkcg3.setGroupId((j+1)+"");
					zkcg3.setSupplierId(sId);
					zkcg3.setSupplierName(sName);
					//保存数据
					yingfudanService.save(zkcg2);
					yingfudanService.save(zkcg3);
					fp.setIsUpload(1);
					finPayableService.updateById(fp);
				}else {
					return Result.error("请不要重复生成凭证");
				}
			}
		}else {
			return Result.error("未找到应付单数据");
		}
		return Result.ok("应付单凭证生成成功");
	}

	/**
	 *  采购付款凭证生成
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "采购付款凭证生成")
	@ApiOperation(value="采购付款凭证生成", notes="采购付款凭证生成")
	@GetMapping(value = "/generateFKD")
	public Result<?> generateFKD(@RequestParam(name="ids",required=true) String ids,@RequestParam(name="kjkm1",required=true) String kjkm1,@RequestParam(name="kjkm2",required=true) String kjkm2){
		List<String> lis=Arrays.asList(ids.split(","));
		String compName="";
		String bankCardNo="";
		String bankname="";
		//查询本企业银行卡信息
		List<BasBankAccount> bbaList=basBankAccountService.list();
		if(null!=bbaList&&bbaList.size()>0) {
			for(BasBankAccount ba:bbaList) {
				if(ba.getName().indexOf("启元")>0) {
					compName=ba.getName();
					bankCardNo=ba.getAccountNo();
					bankname=ba.getBankNo();
				}
			}
		}
		//查询借贷会计科目
		String acJId="";
		String acJName="";
		String acJCode="";
		String acDId="";
		String acDName="";
		String acDCode="";
		Kemu kmj=kemuService.getById(kjkm1);
		Kemu kmd=kemuService.getById(kjkm2);
		if(null!=kmj) {
			acJId=kmj.getId();
			acJName=kmj.getKmmc();
			acJCode=kmj.getKmbh();
		}
		if(null!=kmd) {
			acDId=kmd.getId();
			acDName=kmd.getKmmc();
			acDCode=kmd.getKmbh();
		}
		//采购付款单--凭证
		QueryWrapper<FinPayment> fpQw3=new QueryWrapper<FinPayment>();
		fpQw3.in("id", lis);
		List<FinPayment> fpment2List=finPaymentService.list(fpQw3);
		//非空
		if(null!=fpment2List&&fpment2List.size()>0) {
			for(int j=0;j<fpment2List.size();j++) {
				FinPayment fp=fpment2List.get(j);
				//未生成过凭证
				if(fp.getIsUpload()==0) {
					//查询供应商信息
					BasSupplier bs=basSupplierService.getById(fp.getSupplierId());
					String sId="";
					String sName="";
					try {
						sId=bs.getId();
						sName=bs.getName();
					} catch (Exception e) {
					}
					//构造中间表数据--借
					Fukuandan zkcg2=new Fukuandan();
					zkcg2.setCreateBy("admin");
					zkcg2.setCreateTime(fp.getCreateTime());
					zkcg2.setBillNo(fp.getBillNo());
					zkcg2.setBillType("采购付款单");
					zkcg2.setBillMoney(fp.getAmt());
					zkcg2.setAccountingId(acJId);
					zkcg2.setAccountingType(acJName);
					zkcg2.setKindeeType("借");
					zkcg2.setGroupId((j+1)+"");
					zkcg2.setSupplierId(sId);
					zkcg2.setSupplierName(sName);
					//构造中间表数据--贷
					Fukuandan zkcg3=new Fukuandan();
					zkcg3.setCreateBy("admin");
					zkcg3.setCreateTime(fp.getCreateTime());
					zkcg3.setBillNo(fp.getBillNo());
					zkcg3.setBillType("采购付款单");
					zkcg3.setBillMoney(fp.getAmt());
					zkcg3.setAccountingId(acDId);
					zkcg3.setAccountingType(acDName);
					zkcg3.setKindeeType("贷");
					zkcg3.setGroupId((j+1)+"");
					zkcg3.setSupplierId(sId);
					zkcg3.setSupplierName(sName);
					//保存数据
					fukuandanService.save(zkcg2);
					fukuandanService.save(zkcg3);
					fp.setIsUpload(1);
					finPaymentService.updateById(fp);
				}else {
					return Result.error("请不要重复生成凭证");
				}
			}
		}
		return Result.ok();
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "计件工资表-批量删除")
	@ApiOperation(value="计件工资表-批量删除", notes="计件工资表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.zrkPiecerateService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "计件工资表-通过id查询")
	@ApiOperation(value="计件工资表-通过id查询", notes="计件工资表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ZrkPiecerate zrkPiecerate = zrkPiecerateService.getById(id);
		if(zrkPiecerate==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(zrkPiecerate);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param zrkPiecerate
	 */
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, ZrkPiecerate zrkPiecerate) {
		return zrkPiecerateService.exportXls(request, zrkPiecerate, ZrkPiecerate.class, "计件工资单");
	}


	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, ZrkPiecerate.class);
	}

}
